package com.shujia

import com.mysql.jdbc.Driver

import java.sql.{Connection, DriverManager, ResultSet, Statement}

object Code04Mysql {
  def main(args: Array[String]): Unit = {
      // Scala 连接Mysql 使用的也是Java中的相关类

    DriverManager.registerDriver(new Driver())
    val mysqlCon: Connection = DriverManager.getConnection("jdbc:mysql://master:3306/stu?useSSL=false", "root", "123456")

    val statement: Statement = mysqlCon.createStatement()

    val resultSet: ResultSet = statement.executeQuery(
      """
        |SELECT
        |*
        |FROM students
        |WHERE clazz  like '%理科六班%' AND age = '21'
        |""".stripMargin
    )

    while(resultSet.next()){
      val id: String = resultSet.getString("id")
      val name: String = resultSet.getString("name")
      println(s"$id\t$name")
    }

    statement.close()
    mysqlCon.close()

  }
}
